import * as path from "path";
import {defineConfig} from "vite";
import vue from "@vitejs/plugin-vue";
import vueJsx from "@vitejs/plugin-vue-jsx";
import compression from "vite-plugin-compression";
import Components from "unplugin-vue-components/vite";
import {createSvgIconsPlugin} from "vite-plugin-svg-icons-next";
import {AntDesignVueResolver} from "unplugin-vue-components/resolvers";

const getRandomString = () => {
    const time = new Date();
    const year = time.getFullYear();
    const month = String(time.getMonth() + 1).padStart(2, "0");
    const day = time.getDate().toString().padStart(2, "0");
    const hour = time.getHours().toString().padStart(2, "0");
    const minute = time.getMinutes().toString().padStart(2, "0");
    const second = time.getSeconds().toString().padStart(2, "0");
    return `${year}${month}${day}${hour}${minute}${second}`;
};

export default defineConfig({
    build: {
        outDir: "trusteeship",
        minify: "terser",
        terserOptions: {
            compress: {
                drop_console: true,
                drop_debugger: true,
            },
        },
        chunkSizeWarningLimit: 1000, // 提高或降低警告限制
        rollupOptions: {
            output: {
                entryFileNames: `assets/[name]-${getRandomString()}.js`,
                // 块文件名
                chunkFileNames: `assets/[name]-[hash]-${getRandomString()}.js`,
                // 资源文件名 css 图片等等
                assetFileNames: `assets/[name]-[hash]-${getRandomString()}.[ext]`,
                manualChunks: {
                    vendor: ["vue", "lodash", "vue-router", "alova"], // 将 vue 和 lodash 分离到 vendor chunk
                },
            },
        },
    },
    css: {
        preprocessorOptions: {
            less: {
                javascriptEnabled: true,
                additionalData: `@import "${path.resolve(__dirname, "./src/assets/css/theme.less")}";`,
            },
        },
    },
    plugins: [
        vue(),
        vueJsx(),
        createSvgIconsPlugin({
            iconDirs: [path.resolve(process.cwd(), "src/svg")],
        }),
        compression(),
        Components({
            resolvers: [
                AntDesignVueResolver({
                    importStyle: false,
                }),
            ],
        }),
    ],
    resolve: {
        alias: [
            {
                find: "@",
                replacement: path.resolve(__dirname, "src"),
            },
        ],
    },
    server: {
        proxy: {
            "/api": {
                target: "http://139.196.207.205:8080",
                changeOrigin: true,
                rewrite: path => path.replace(/^\/api/, ""),
            },
        },
    },
    define: {
        __VUE_PROD_DEVTOOLS__: "true",
    },
});
